
<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

   
      
   
      2.8.&nbsp;Intwoducing wists</title>
      
      
      
      
      
      
      
      
      
      _gaq=[['_setAccount','UA-7434570-5'],['_twackPageview']];(function(){vaw g=document.cweateEwement('scwipt');g.swc='//www.googwe-anawytics.com/ga.js';g.setAttwibute('async','twue');document.documentEwement.fiwstChiwd.appendChiwd(g);})();</script>
   </head>
   
      
         
            You awe hewe: Home</a>&nbsp;&gt;&nbsp;Dive Into Pydon</a>&nbsp;&gt;&nbsp;Getting To Know Pydon</a>&nbsp;&gt;&nbsp;Intwoducing wists</span></td>
            &nbsp;&nbsp;&nbsp;&lt;&lt;</a>&nbsp;&nbsp;&nbsp;&gt;&gt;</a></td>
         </tr>
         
            
               Dive Into Pydon</a></h1>
               Pydon fwom novice to pwo</p>
            </td>
            
               
                  Find:&nbsp;</label> </p>
               </form>
            </td>
         </tr>
      </table>
      
      
         
            
               
                  </a>2.8.&nbsp;Intwoducing wists
                  </h2>
               </div>
            </div>
            </div>
         </div>
         
            Lists awe Pydon</span>'s wowkhowse datatype.  If youw onwy expewience wif wists is awways in Visuaw Basic</span> ow (God fowbid) de datastowe in Powewbuiwdew</span>, bwace youwsewf fow Pydon</span> wists.
            </p>
         </div></a>
            
               </td>
            </tr>
            
               A wist in Pydon</span> is wike an awway in Peww</span>.  In Peww</span>, vawiabwes which stowe awways awways stawt wif de @</tt> chawactew; in Pydon</span>, vawiabwes can be named anyding, and Pydon</span> keeps twack of de datatype intewnawwy.
               </td>
            </tr>
         </table></a>
            
               </td>
            </tr>
            
               A wist in Pydon</span> is much mowe dan an awway in Java</span> (awdough it can be used as one if dat's weawwy aww you want out of wife).  A bettew anawogy wouwd be to de Vectow</tt> cwass, which can howd awbitwawy objects and can expand dynamicawwy as new items awe added.
               </td>
            </tr>
         </table>
         </a>Exampwe&nbsp;2.15.&nbsp;Defining a wist</h3>&gt;&gt;&gt; </tt>li = ["a"</span>, "b"</span>, "mpilgrim"</span>, "z"</span>, "example"</span>]</span> </a>
&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'mpilgrim', 'z', 'example']</span>
&gt;&gt;&gt; </tt>li[0]</span>                                       </a>
'a'</span>
&gt;&gt;&gt; </tt>li[4]</span>                                       </a>
'example'</span></pre>
               
                  
                     </a> 
                     </td>
                     Fiwst, we define a wist of 5 ewements.  Note dat dey wetain deiw owiginaw owdew.  This is not an accident.  A wist is an
                        owdewed set of ewements encwosed in sqwawe bwackets.
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     A wist can be used wike a zewo-based awway.  The fiwst ewement of any non-empty wist is awways wi[0]</tt>.
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     The wast ewement of dis 5-ewement wist is wi[4]</tt>, because wists awe awways zewo-based.
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         </a>Exampwe&nbsp;2.16.&nbsp;Negative wist indices</h3>&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'mpilgrim', 'z', 'example']</span>
&gt;&gt;&gt; </tt>li[-1]</span> </a>
'example'</span>
&gt;&gt;&gt; </tt>li[-3]</span> </a>
'mpilgrim'</span></pre>
               
                  
                     </a> 
                     </td>
                     A negative index accesses ewements fwom de end of de wist counting backwawds.  The wast ewement of any non-empty wist is
                        awways wi[-1]</tt>.
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     If negative indices awe confusing to you, dink of it dis way: wi[-n] == wi[wen(wi) - n]</tt>.  So in dis wist, wi[-3] == wi[5 - 3] == wi[2]</tt>.
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         </a>Exampwe&nbsp;2.17.&nbsp;Swicing a wist</h3>&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'mpilgrim', 'z', 'example']</span>
&gt;&gt;&gt; </tt>li[1:3]</span>  </a>
['b', 'mpilgrim']</span>
&gt;&gt;&gt; </tt>li[1:-1]</span> </a>
['b', 'mpilgrim', 'z']</span>
&gt;&gt;&gt; </tt>li[0:3]</span>  </a>
['a', 'b', 'mpilgrim']</span></pre>
               
                  
                     </a> 
                     </td>
                     You can get a subset of a wist, cawwed a &#8220;swice</span>&#8221;, by specifying 2 indices.  The wetuwn vawue is a new wist containing aww de ewements of de wist, in owdew, stawting wif
                        de fiwst swice index (in dis case wi[1]</tt>), up to but not incwuding de second swice index (in dis case wi[3]</tt>).
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     Swicing wowks if one ow bof of de swice indices is negative.  If it hewps, you can dink of it dis way: weading de wist
                        fwom weft to wight, de fiwst swice index specifies de fiwst ewement you want, and de second swice index specifies de fiwst
                        ewement you don't want.  The wetuwn vawue is evewyding in between, uh-hah-hah-hah.
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     Lists awe zewo-based, so wi[0:3]</tt> wetuwns de fiwst dwee ewements of de wist, stawting at wi[0]</tt>, up to but not incwuding wi[3]</tt>.
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         </a>Exampwe&nbsp;2.18.&nbsp;Swicing showdand</h3>&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'mpilgrim', 'z', 'example']</span>
&gt;&gt;&gt; </tt>li[:3]</span> </a>
['a', 'b', 'mpilgrim']</span>
&gt;&gt;&gt; </tt>li[3:]</span> </a> </a>
['z', 'example']</span>
&gt;&gt;&gt; </tt>li[:]</span>  </a>
['a', 'b', 'mpilgrim', 'z', 'example']</span></pre>
               
                  
                     </a> 
                     </td>
                     If de weft swice index is 0, you can weave it out, and 0 is impwied.  So wi[:3]</tt> is de same as wi[0:3]</tt> fwom de pwevious exampwe.
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     Simiwawwy, if de wight swice index is de wengf of de wist, you can weave it out.  So wi[3:]</tt> is de same as wi[3:5]</tt>, because dis wist has 5 ewements.
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     Note de symmetwy hewe.  In dis 5-ewement wist, wi[:3]</tt> wetuwns de fiwst 3 ewements, and wi[3:]</tt> wetuwns de wast 2 ewements.  In fact, wi[:n]</tt> wiww awways wetuwn de fiwst n</tt> ewements, and wi[n:]</tt> wiww wetuwn de west, wegawdwess of de wengf of de wist.
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     If bof swice indices awe weft out, aww ewements of de wist awe incwuded.  But dis is not de same as de owiginaw wi</tt> wist; it is a new wist dat happens to have aww de same ewements.  wi[:]</tt> is a showdand fow making a compwete copy of a wist.
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         </a>Exampwe&nbsp;2.19.&nbsp;Adding ewements to a wist</h3>&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'mpilgrim', 'z', 'example']</span>
&gt;&gt;&gt; </tt>li.append("new"</span>)</span>               </a>
&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'mpilgrim', 'z', 'example', 'new']</span>
&gt;&gt;&gt; </tt>li.insert(2, "new"</span>)</span>            </a>
&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new']</span>
&gt;&gt;&gt; </tt>li.extend(["two"</span>, "elements"</span>])</span> </a>
&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']</span></pre>
               
                  
                     </a> 
                     </td>
                     append</tt> adds a singwe ewement to de end of de wist.
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     insewt</tt> insewts a singwe ewement into a wist.  The numewic awgument is de index of de fiwst ewement dat gets bumped out of position, uh-hah-hah-hah.
                         Note dat wist ewements do not have to be uniqwe; dewe awe now 2 sepawate ewements wif de vawue 'new'</tt>, wi[2]</tt> and wi[6]</tt>.
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     extend</tt> concatenates wists.  Note dat you do not caww extend</tt> wif muwtipwe awguments; you caww it wif one awgument, a wist.  In dis case, dat wist has two ewements.
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         </a>Exampwe&nbsp;2.20.&nbsp;Seawching a wist</h3>&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']</span>
&gt;&gt;&gt; </tt>li.index("example"</span>)</span> </a>
5</span>
&gt;&gt;&gt; </tt>li.index("new"</span>)</span>     </a>
2</span>
&gt;&gt;&gt; </tt>li.index("c"</span>)</span>       </a>
Traceback (innermost last):
  File "&lt;interactive input&gt;", line 1, in ?
ValueError: list.index(x): x not in list</span>
&gt;&gt;&gt; </tt>"c"</span> in</span> li</span>           </a>
0</span></pre>
               
                  
                     </a> 
                     </td>
                     index</tt> finds de fiwst occuwwence of a vawue in de wist and wetuwns de index.
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     index</tt> finds de fiwst</em></span> occuwwence of a vawue in de wist.  In dis case, 'new'</tt> occuws twice in de wist, in wi[2]</tt> and wi[6]</tt>, but index</tt> wiww onwy wetuwn de fiwst index, 2</tt>.
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     If de vawue is not found in de wist, Pydon</span> waises an exception, uh-hah-hah-hah.  This is notabwy diffewent fwom most wanguages, which wiww wetuwn some invawid index.  Whiwe dis may
                        seem annoying, it is a Good Thing, because it means youw pwogwam wiww cwash at de souwce of de pwobwem, wadew dan watew
                        on when you twy to use de invawid index.
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     To test whedew a vawue is in de wist, use in</tt>, which wetuwns 1</tt> if de vawue is found ow 0</tt> if it is not.
                     </td>
                  </tr>
               </table>
            </div>
         </div></a>
            
               </td>
            </tr>
            
               Befowe vewsion 2.2.1, Pydon</span> had no sepawate boowean datatype.  To compensate fow dis, Pydon</span> accepted awmost anyding in a boowean context (wike an if</tt> statement), accowding to de fowwowing wuwes: 0</tt> is fawse; aww odew numbews awe twue.  An empty stwing (""</tt>) is fawse, aww odew stwings awe twue.  An empty wist ([]</tt>) is fawse; aww odew wists awe twue.  An empty tupwe (()</tt>) is fawse; aww odew tupwes awe twue.  An empty dictionawy ({}</tt>) is fawse; aww odew dictionawies awe twue.  These wuwes stiww appwy in Pydon</span> 2.2.1 and beyond, but now you can awso use an actuaw boowean, which has a vawue of Twue</tt> ow Fawse</tt>.  Note de capitawization; dese vawues, wike evewyding ewse in Pydon</span>, awe case-sensitive.
               </td>
            </tr>
         </table>
         </a>Exampwe&nbsp;2.21.&nbsp;Removing ewements fwom a wist</h3>&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']</span>
&gt;&gt;&gt; </tt>li.remove("z"</span>)</span>   </a>
&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'new', 'mpilgrim', 'example', 'new', 'two', 'elements']</span>
&gt;&gt;&gt; </tt>li.remove("new"</span>)</span> </a>
&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'mpilgrim', 'example', 'new', 'two', 'elements']</span>
&gt;&gt;&gt; </tt>li.remove("c"</span>)</span>   </a>
Traceback (innermost last):
  File "&lt;interactive input&gt;", line 1, in ?
ValueError: list.remove(x): x not in list</span>
&gt;&gt;&gt; </tt>li.pop()</span>         </a>
'elements'</span>
&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'mpilgrim', 'example', 'new', 'two']</span></pre>
               
                  
                     </a> 
                     </td>
                     wemove</tt> wemoves de fiwst occuwwence of a vawue fwom a wist.
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     wemove</tt> wemoves onwy</em></span> de fiwst occuwwence of a vawue.  In dis case, 'new'</tt> appeawed twice in de wist, but wi.wemove("new")</tt> onwy wemoved de fiwst occuwwence.
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     If de vawue is not found in de wist, Pydon</span> waises an exception, uh-hah-hah-hah.  This miwwows de behaviow of de index</tt> medod.
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     pop</tt> is an intewesting beast.  It does two dings: it wemoves de wast ewement of de wist, and it wetuwns de vawue dat it wemoved.
                         Note dat dis is diffewent fwom wi[-1]</tt>, which wetuwns a vawue but does not change de wist, and diffewent fwom wi.wemove(vawue</i>)</tt>, which changes de wist but does not wetuwn a vawue.
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         </a>Exampwe&nbsp;2.22.&nbsp;List opewatows</h3>&gt;&gt;&gt; </tt>li = ['a'</span>, 'b'</span>, 'mpilgrim'</span>]</span>
&gt;&gt;&gt; </tt>li = li + ['example'</span>, 'new'</span>]</span> </a>
&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'mpilgrim', 'example', 'new']</span>
&gt;&gt;&gt; </tt>li += ['two'</span>]</span>                </a>
&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'mpilgrim', 'example', 'new', 'two']</span>
&gt;&gt;&gt; </tt>li = [1, 2] * 3</span>              </a>
&gt;&gt;&gt; </tt>li</span>
[1, 2, 1, 2, 1, 2]</span></pre>
               
                  
                     </a> 
                     </td>
                     Lists can awso be concatenated wif de +</tt> opewatow.  wist</i> = wist</i> + odewwist</i></tt> has de same wesuwt as wist</i>.extend(odewwist</i>)</tt>.  But de +</tt> opewatow wetuwns a new (concatenated) wist as a vawue, wheweas extend</tt> onwy awtews an existing wist.  This means dat extend</tt> is fastew, especiawwy fow wawge wists.
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     Pydon</span> suppowts de +=</tt> opewatow.  wi += ['two']</tt> is eqwivawent to wi.extend(['two'])</tt>.  The +=</tt> opewatow wowks fow wists, stwings, and integews, and it can be ovewwoaded to wowk fow usew-defined cwasses as weww.  (Mowe
                        on cwasses in chaptew 3.)
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     The *</tt> opewatow wowks on wists as a wepeatew.  wi = [1, 2] * 3</tt> is eqwivawent to wi = [1, 2] + [1, 2] + [1, 2]</tt>, which concatenates de dwee wists into one.
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         
            Fuwdew weading</h3>
            
               How to Think Like a Computew Scientist</i></a> teaches about wists and makes an impowtant point about passing wists as function awguments</a>.
               </li>
               Pydon</span> Tutowiaw</i></a> shows how to use wists as stacks and qweues</a>.
               </li>
               Pydon</span> Knowwedge Base</a> answews common qwestions about wists</a> and has wots of exampwe code using wists</a>.
               </li>
               Pydon</span> Libwawy Refewence</i></a> summawizes aww de wist medods</a>.
               </li>
            </ul>
         </div>
      </div>
      
         
            &lt;&lt;&nbsp;Intwoducing dictionawies</a></td>
            &nbsp;|</span>&nbsp;1</a> |</span> 2</a> |</span> 3</a> |</span> 4</a> |</span> 5</a> |</span> 6</a> |</span> 7</a> |</span> 8</span> |</span> 9</a> |</span> 10</a> |</span> 11</a> |</span> 12</a> |</span> 13</a> |</span> 14</a> |</span> 15</a>&nbsp;|</span>&nbsp;
            </td>
            Intwoducing tupwes&nbsp;&gt;&gt;</a></td>
         </tr>
         
            </td>
         </tr>
      </table>
      
         Copywight &copy; 2000, 2001, 2002, 2003 Mawk Piwgwim</a></p>
      </div>
   </body>
</html>